package com.mc.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mc.model.Path;
import com.mc.model.Xxydtype;
import com.mc.model.Xyyd;
import com.mc.service.IXXydService;
import com.mc.service.IXxydtypeService;
import com.mc.utils.PathUtil;

@Controller
@RequestMapping("/learning")
public class LearningAction {

	@Autowired private IXxydtypeService xxydtypeService;
	@Autowired private IXXydService xydService;
	private Path path;
	/**
	 * 
	 * @param modelMap
	 * @param pagenow 当前页
	 * @param type 学习园地
	 * @return 学习园地界面
	 */
	@RequestMapping("/index_{type}/{pagenow}")
	public String index(ModelMap modelMap,@PathVariable("pagenow")int pagenow,@PathVariable("type")int type){
		int pbulletincount=  xydService.countofxxydbytype(type);
		Xxydtype xxydtype = xxydtypeService.selectByPrimaryKey(type);
		List<Xxydtype> xxydtypes = null;
		int pagecount = 0;
		List<Xyyd> xxyds = null;
		int pagesize = 6;
		xxydtypes = xxydtypeService.xxydtypes();
		if(pbulletincount != 0){
			pagecount = getPageCount(pbulletincount, pagesize);
			xxyds = xydService.xxydsbytype(type,(pagenow-1)*pagesize, pagesize);
		}
		modelMap.addAttribute("xxydtype", xxydtype);
		modelMap.addAttribute("xxyds", xxyds);
		modelMap.addAttribute("xxydtypes", xxydtypes);
		modelMap.addAttribute("pagenow",pagenow);
		modelMap.addAttribute("pagecount",pagecount);
		path = PathUtil.setPathParams(new String[]{
				"PackageName:Learning",
				"ViewName:wenhuazu",
				"ViewTitle:学习园地",
				"smenu:xxydindex",
				"mmenu:"+xxydtype.getXyydtypeName(),
				"mmmenu:"+type});				
		return PathUtil.returnStr(path, modelMap);
	}
	public int getPageCount(int allcount,int pagesize)
	{
		//System.out.println("allcount---" + allcount + "--pagesize----");
		int pagecount = 0;
		if(allcount%pagesize == 0)
		{
			pagecount = allcount/pagesize;
		}else{
			pagecount = allcount/pagesize + 1;
		}
		return pagecount;
	}
	/**
	 * 
	 * @param id 学习园地文章id
	 * @param modelMap
	 * @return 学习园地展示页面
	 */
	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable("id") int id,ModelMap modelMap){
		Xyyd xyyd = xydService.selectByPrimaryKey(id);
		List<Xxydtype> xxydtypes = xxydtypeService.xxydtypes();
		modelMap.addAttribute("xxydtypes", xxydtypes);
		Xxydtype xxydtype  = xxydtypeService.selectByPrimaryKey(xyyd.getXyydtId().getXyydtypeId());
		modelMap.addAttribute("xxydtype", xxydtype);
		modelMap.addAttribute("xyydinfo", xyyd);
		//System.out.println(xyyd.toString());
		path = PathUtil.setPathParams(new String[]{"PackageName:Learning","ViewName:detail","ViewTitle:学习园地","smenu:xxydindex","mmenu:"+xxydtype.getXyydtypeName(),"mmmenu:"+xyyd.getXyydtId().getXyydtypeId()});				
		return PathUtil.returnStr(path, modelMap);
	}
}
